package myjava.i_collection;

import java.util.*;

/**
 * @Auther: cty
 * @Date: 2019/11/16 下午3:13
 * @Description: 测试表格数据的存储
 * @version: 1.0
 */
public class O_TestStoreData {
    public static void main(String[] args) {
//        TestORM1();
        TestORM2();
    }

    /* ORM思想简单实验：map表示一行数据，多行数据是多个map；将多个map放到list中 */
    public static void TestORM1(){
        Map<String,Object> row1 = new HashMap<>();
        row1.put("id",1001);
        row1.put("姓名","张三");
        row1.put("薪水",20000);
        row1.put("入职日期","2018.5.5");

        Map<String,Object> row2 = new HashMap<>();
        row2.put("id",1002);
        row2.put("姓名","李四");
        row2.put("薪水",30000);
        row2.put("入职日期","2005.4.4");

        Map<String,Object> row3 = new HashMap<>();
        row3.put("id",1003);
        row3.put("姓名","王五");
        row3.put("薪水",3000);
        row3.put("入职日期","2020.5.4");

        List<Map<String,Object>> table1 = new ArrayList<>();
        table1.add(row1);
        table1.add(row2);
        table1.add(row3);

        for(Map<String,Object> row:table1){
            Set<String> keySet = row.keySet();
            for(Iterator<String> iter=keySet.iterator();iter.hasNext();){
                String key = iter.next();
                System.out.print(key+":"+row.get(key)+"\t");
            }
            System.out.println();
        }
    }

    /* ORM思想简单实验：javabean对象表示一行数据，多行放在map或list中 */
    public static void TestORM2(){

        User user1 = new User(1001,"张三",20000,"2018.5.5");
        User user2 = new User(1002,"李四",30000,"2005.4.4");
        User user3 = new User(1003,"王五",3000,"2020.5.4");

        //多行javabean对象放在List
        List<User> list  = new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user3);

        for(User u:list){
            System.out.println(u);
        }

        //多行javabean对象放在Map
        Map<Integer,User> map = new HashMap<>();
        map.put(1001,user1);
        map.put(1002,user2);
        map.put(1003,user3);
        Set<Integer> set = map.keySet();
        for(Integer key:set){
            System.out.println(key+"---"+map.get(key));
        }

    }
}

// 定义一个javabean，要有set和get方法以及无参构造器
class User{
    private int id;
    private String name;
    private double salary;
    private String hirtime;

    public User(int id, String name, double salary, String hirtime) {
        this.id = id;
        this.name = name;
        this.salary = salary;
        this.hirtime = hirtime;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public String getHirtime() {
        return hirtime;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public void setHirtime(String hirtime) {
        this.hirtime = hirtime;
    }

    @Override
    public String toString() {
        return "id:"+id+"\tname:"+name+"\tsalary:"+salary+"\thairtime:"+hirtime;
    }
}
